package com.cmccgz.baselibrary.base;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author king
 * @date 2017/3/15 17:24
 */

public class ThreadManager {
    private ExecutorService service;

    private ThreadManager() {
        int num = Runtime.getRuntime().availableProcessors();
        service = Executors.newFixedThreadPool(num * 2);
    }

    private static ThreadManager manager;

    public static ThreadManager getInstance() {
        if (manager == null) {
            synchronized (ThreadManager.class) {
                if (manager == null) {
                    manager = new ThreadManager();
                }
            }
        }
        return manager;
    }

    public void addTask(Runnable runnable) {
        service.execute(runnable);
    }

    public void cancelTask() {
        service.shutdown();
    }

}
